const express = require("express");
// const  App=express();
//导入操作server的数据模型
const serverModel = require("../model/chatServerModel");
//引入数据库
const mysql = require('mysql');
const config = require('../config/config')
//实现本地链接
// console.log(config.host)
const connection = mysql.createConnection({
    host: config.host,
    user: config.user,
    password: config.password,
    multipleStatements: true,
    database: config.database
})

connection.connect((err) => {
    if (err) throw err;
    console.log("数据库连接成功0")
})


// 第一种数据库操作1
const ldz2FristWrite = (req, res) => {
    connection.query('INSERT INTO dayyeild SET  ?', { allh: 21222, H0910: 122 }, function (err, data) {
        if (err) {
            console.log('插入数据失败1')
        } else {
            console.log('插入数据成功1');
            connection.query('SELECT * FROM  dayyeild', function (err, data) {
                if (err) {
                    console.log('查询数据失败1',err);
                } else {
                    console.log("写入成功1，查询结果：", data)
                    //   connection.end() ;

                }
            })
        }
    })
}


// 第二中写入数据库
const WriteDayYeild = (req, res) => {
    const sql = `INSERT INTO  dayyeild SET ?`;
    let writeData = { H1112: 12222, H0809: 32322 };
    connection.query(sql, writeData, (err, result) => {
        if (err) {
            console.log("写入数据库错误2", err)
        } else {
            console.log("写入成功2")
            //  res.send("写入数据库成功2")
        }
    })
}

    // 第三种数据写入3
// 保存生产数据函数
function ld_z2(table,adress, jsonData) {
    console.log("调用ldz2")
    serverModel.dayInsert(table,adress, jsonData).then((res) => {
        console.log('保存/产量成功3')
    }).catch((err) => {
        console.log("保存/产量失败！3", err);
    });
}

module.exports = {
    WriteDayYeild,
    ld_z2,
    ldz2FristWrite

}